package com.xdja.testxlog;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class lyq {
    public static void main(String args[]) {
        System.out.println("Hello World!");

        testArray();

    }

    public static void  testArray(){
        int count = 41;
        int eviil = 5;
        ArrayList<Integer> arrayList = new ArrayList<>();
        for(int i = 0;i<count;i++){
            arrayList.add(i+1);
        }
        callNumMethod(arrayList,eviil);
    }
    public static void callNumMethod(ArrayList<Integer> array, int M){
        int count = array.size();
        int eviil = M;
        int curIndex=0;
        int number=0;
        ArrayList<Integer>  nextList = new ArrayList<>();
        if(count<eviil) {
            for(curIndex = 0;array.size()>1;){
                number++;

                if(number==eviil){
                    array.remove(curIndex);
                    number = 0;
                   continue;
                }
                  curIndex++;
                if (curIndex >(array.size() - 1)) {
                    curIndex = 0;
                }

            }
            if(array.size()==1){
                System.out.println("留下的人是："+array.get((array.size()-1)));
            }
            return;
        }
        int insertCout = count%eviil;
        if(insertCout>0) {
            for(int i =insertCout;i>0;i--) {
                nextList.add(array.get(count - i));
            }
        }
        count = count-insertCout;
        for(curIndex = 0;curIndex<count;curIndex++){
            number++;
            System.out.println("number是："+number);
            if(count<eviil) {
                for(curIndex = 0;array.size()>1;){
                    number++;

                    if(number==eviil){
                      //  out[++outIndex] = array.get(curIndex);//记录出列编号
                        array.remove(curIndex);
                        number = 0;
                        continue;
                    }
                    curIndex++;
                    if (curIndex > (array.size() - 1)) {
                        curIndex = 0;
                    }
                }
                if(array.size()==1){
                    System.out.println("留下的人是："+array.get((array.size()-1)));
                }
                break;
            }
            if(number==eviil){
              //  out[++outIndex] = array.get(curIndex);//记录出列编号
                number = 0;
                System.out.println("离开的人是："+array.get(curIndex));
                continue;
            }else {
                System.out.println("curIndex是："+curIndex);
                nextList.add(array.get(curIndex));
            }
        }
        if(nextList.size()>1){
            array.clear();
            callNumMethod(nextList,eviil);
        }else {
            System.out.println("留下的人是："+nextList.get((nextList.size()-1)));
        }
    }
}
